<template>
  <div class="module-list">
    <a-list
      :bordered="false"
      :dataSource="moduleList"
      :locale="{ emptyText: '暂无数据' }"
    >
      <template #header>
        <div class="list-header">
          <span class="taskHeadline">制品列表</span>
          <a-button size="small" type="primary" @click="toAdd">
            <template #icon><PlusOutlined /> </template>
          </a-button>
        </div>
      </template>
      <template #renderItem="{ item }">
        <a-list-item :class="item.id===moduleId?'checkItem' : 'unCheckItem'"
          @click="changeModule(item.id)">
          <template #actions>
            <a-popconfirm
              :visible="item.deleteVisible"
              title="您确定要删除制品吗？"
              ok-text="确定"
              cancel-text="取消"
              @confirm="remove(item.id)"
              @cancel="cancelRemove(item)"
            />
            <a-button size="small"  @click="toRemove(item)" style="border:none"
              >
            <template #icon><DeleteOutlined /> </template></a-button
            >
          </template>
          <a-list-item-meta :description="item.name"> </a-list-item-meta>
        </a-list-item>
      </template>
    </a-list>
    <a-modal
      title="新增制品"
      v-model:visible="state.addVisible"
      width="35%"
      cancelText="取消"
      okText="确认"
      :maskClosable="false"
      :onOk="add"
    >
      <div class="addModuleContainer">
        <a-form ref="formRef" :rules="state.rules" :model="state.addForm">
          <a-form-item
            label="制品名称"
            :label-col="{ span: 3 }"
            :wrapper-col="{ span: 10 }"
            name="name"
            :rules="[{ required: true, message: '请输入制品名称!' }, { message: '请输入正确的制品名称', pattern: /^[\w-]{1,20}$/ }]"
          >
            <a-input style="margin-left: 10px;" v-model:value="state.addForm.name"> </a-input>
          </a-form-item>
          <a-form-item
            label="制品类型"
            :label-col="{ span: 3 }"
            :wrapper-col="{ span: 21 }"
            class="new"
            name="type"
          >
            <a-select style="width: 241px;margin-left: 10px;" v-model:value="state.addForm.type">
              <a-select-option value="exec_jar" v-if="'java' === project.type">exec jar</a-select-option>
              <a-select-option value="deploy_jar" v-if="'java' === project.type">deploy jar</a-select-option>
              <a-select-option value="static" v-if="'js' === project.type">static</a-select-option>
            </a-select>
          </a-form-item>
        </a-form>
      </div>
    </a-modal>
  </div>
</template>



<script lang="ts">
import { defineComponent, toRefs, reactive, ref } from "vue";
import { moduleApi } from "@/api/devcloudApi";
import { message, FormInstance } from "ant-design-vue";
import { PlusOutlined, DeleteOutlined } from "@ant-design/icons-vue";
import store from "@/store";

export default defineComponent({
  name: "moduleList",
  components: {
    PlusOutlined,
    DeleteOutlined
  },
  props: {
    moduleList: {
      type: Array,
      required: true,
    },
    project: {
      type: Object,
      required: false,
    },
    moduleId: {
      type: Number,
      required: false,
    },
  },
  emits: ["reloadModuleBuildRun", "reloadModuleList"],
  setup(props, ctx) {
    const formRef = ref<FormInstance>()
    const { moduleList, project, moduleId } = toRefs(props);
    const state = reactive({
      addForm: {},
      addVisible: false,
      removeitem: {
        id: ''
      },
      rules: {
        name: [
          { required: true, validateTrigger: "onBlur", message:"请输入制品名称"},
        ],
        type: [
          { required: true, message:"请选择制品类型"},
        ],
      }
    });
    const initAddForm = () => {
      if (!project.value) {
        message.error("未选择项目");
        return;
      }
      state.addForm = {
        projectId: project.value.id,
        envId: store.getters.getCurrInfo.envId
      }
    }

    const toAdd = () => {
      initAddForm();
      state.addVisible = true;
    }

    const cancelAdd = () => {
      state.addVisible = false;
    }

    const add = async () => {
      formRef.value?.validateFields().then(async (item) =>{
        const res = await moduleApi.insert(state.addForm);
        if (res.success) {
          cancelAdd();
          message.info('添加成功')
          ctx.emit("reloadModuleList", project.value, res.result);
        }
      }).catch(e=>{
      })
    }

    const remove = async () => {
      const res = await moduleApi.delete(state.removeitem.id);
      if (res.success) {
        cancelAdd();
        message.info('删除成功')
        ctx.emit("reloadModuleList", project.value, moduleId.value);
      }
      cancelRemove(state.removeitem)
    }
    const toRemove = (item:any) => {
      state.removeitem = item
      item.deleteVisible = true;
    }
    const cancelRemove = (item:any) => {
      item.deleteVisible = false;
    }

    const changeModule = (mId:number) =>{
      if(mId === moduleId.value){
        return
      }
      ctx.emit("reloadModuleBuildRun", mId);
    }

    return {
      moduleList,
      moduleId,
      state,
      formRef,
      toAdd,
      cancelAdd,
      add,
      remove,
      toRemove,
      cancelRemove,
      changeModule
    }
  },
});
</script>

<style lang="less" scoped>
.module-list{
  .list-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 24px !important;
  }

  .ant-list-item {
    display: flex;
    text-align: start;
    padding: 15px 18px !important;
    border-bottom: 1px solid #f0f0f0 !important;
  }
  .checkItem {
    height:58px;
    cursor:pointer;
    background-color: hsla(0, 0, 87.8%, 0.5);
    
  }
  .unCheckItem {
    height:58px;
    cursor:pointer;
  }

}
</style>